/**
 * Class: AchievementDetails.java
 * @author Adam Phung
 * Date Created: 10/29/12
 * Date Modified: 11/1/12
 * Description: Pane provides more details of selected achievement
 */

package edu.bu.cs673.superkids123.children.view.components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.common.model.Achievement;

public class AchievementDetails extends JPanel {
	
	/**
	 * Suppress Warning
	 */
	private static final long serialVersionUID = 1L;
	private static final String TEXT_FORMAT = "<html><body><center><br/><i>%s</i><br />%s \u2605<br/>%s</body></center></html>";
	private static final int RIGHT_BORDER_GAP = 5;
	
	private Achievement selectedAchievement;	
	
	private JLabel title;
	private JLabel selectedAchievementLabel = new JLabel("Select an achievement");
	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance(); 
	
	public AchievementDetails() {
		super();
		this.setLayout(new BorderLayout());
		
		// Achievement Title
		title = new JLabel();
		
		// Achievement
		selectedAchievementLabel.setHorizontalAlignment(JLabel.CENTER);
		
		if (selectedAchievement == null) {
			selectedAchievementLabel.setIcon(null);
		} else {
			selectedAchievementLabel.setIcon(selectedAchievement.getMedalImage());
		}
		selectedAchievementLabel.setVerticalTextPosition(JLabel.BOTTOM);
		selectedAchievementLabel.setHorizontalTextPosition(JLabel.CENTER);
		
		this.add(title, BorderLayout.NORTH);
		this.add(selectedAchievementLabel, BorderLayout.CENTER);
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		
		ImageIcon img = provider.getScaledImage(ChildrenResource.imageWoodPlaque, 0.65f);
		g2d.drawImage(img.getImage(), 25, 0, null);
	}

	public Achievement getSelectedAchievement() {
		return selectedAchievement;
	}

	public void setSelectedAchievement(Achievement selectedAchievement) {
		this.selectedAchievement = selectedAchievement;
		refresh();
	}
	
	private void refresh() {
		
		String prize = "";
		if (!selectedAchievement.getPrize().isEmpty())
			prize = String.format("Prize: %s", selectedAchievement.getPrize());
		
		String text = String.format(TEXT_FORMAT, 
				selectedAchievement.getDescription(),
				selectedAchievement.getPoints(),
				prize);
		
		selectedAchievementLabel.setText(text);
		
		title.setText(selectedAchievement.getName());
		title.setPreferredSize(new Dimension(100, 65));
		title.setFont(new Font("Copperplate Gothic Bold", Font.PLAIN, 20));
		title.setHorizontalAlignment(JLabel.CENTER);
		title.setVerticalAlignment(JLabel.BOTTOM);
		
		if (selectedAchievement == null) {
			selectedAchievementLabel.setIcon(provider.getImage(ChildrenResource.imageQuestionMark));
		} else {
			selectedAchievementLabel.setIcon(selectedAchievement.getMedalImage());
		}
	}
}
